#!/usr/bin/env sh
DISPLAYNAME=" Route: via Mobroute directions"
[ -z "$MOBSQLFILTER" ] && MOBSQLFILTER="{glob: nyc subway}"
[ -z "$MOBROUTEMEPODEBUG" ] && MOBROUTEMEPODEBUG="1"

main() {
  FROMTO="$(nominatim_gui_lookup.sh 2)"

  FROM="$(
    echo "$FROMTO" | 
    head -n1 |
    grep -oE '[-0-9.]+' |
    tail -n2 |
    tr "\n" " "
  )"
  FROMLAT="$(echo $FROM | cut -d' ' -f1)"
  FROMLON="$(echo $FROM | cut -d' ' -f2)"

  TO="$(
    echo "$FROMTO" | 
    tail -n1 |
    grep -oE '[-0-9.]+' |
    tail -n2 |
    tr "\n" " "
  )"
  TOLAT="$(echo $TO | cut -d' ' -f1)"
  TOLON="$(echo $TO | cut -d' ' -f2)"

  MOBROUTERESULT="$(
    mobroute \
      -f "$MOBSQLFILTER" \
      -cmd route \
      -format mepolang \
      -lat "$FROMLAT" \
      -lon "$FROMLON" \
      -dlat "$TOLAT" \
      -dlon "$TOLON"
  )"

  if [ "$MOBROUTEMEPODEBUG" = 1 ]; then
    echo "$FROMLAT $FROMLON -> $TOLAT $TOLON" > /tmp/mobroutedebug
    echo "$MOBROUTERESULT" >> /tmp/mobroutedebug
   fi
  echo "$MOBROUTERESULT"
}

main
